package me.trofoto.romeo;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.Weeks;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import me.trofoto.romeo.bean.DrinkCalendar;

@Controller
public class MainController {

	private static final int MIN_DRINK_QUANTITY = 2000;
	private static final int BEST_DRINK_QUANTITY = 2500;
	private static final int MAX_DRINK_QUANTITY = 3000;

	@GetMapping("")
	public String index(Model model) {
		LocalDate today = LocalDate.now();
		LocalDate firstDay = today.dayOfWeek().withMinimumValue();
		LocalDate lastDay = today.dayOfMonth().withMaximumValue().dayOfWeek().withMaximumValue();
		DrinkCalendar drinkCalendar = new DrinkCalendar(today);
		int weeks = Weeks.weeksBetween(firstDay, lastDay).getWeeks() + 1;
		LocalDate[][] calendarDays = new LocalDate[weeks][];
		for (int week = 0, dayPlus = 0; week < weeks; week++) {
			LocalDate[] weekDays = new LocalDate[7];
			for (int day = 0; day < 7; day++) {
				weekDays[day] = firstDay.plusDays(dayPlus);
				dayPlus++;
			}
			calendarDays[week] = weekDays;
		}
		drinkCalendar.setDays(calendarDays);
		model.addAttribute("drinkCalendar", drinkCalendar);

		System.out.println(firstDay);
		System.out.println(lastDay);

		System.out.println(drinkCalendar);

		return "index";
	}

}
